package cn.jsp.spring.jdbc;

import cn.jsp.spring.domain.Stu;
import lombok.SneakyThrows;

import java.lang.reflect.Field;
import java.sql.ResultSet;

public class GeneralRowProcessor<T> implements RowProcessor<T>{
  Class<T> target;

  public GeneralRowProcessor(Class<T> target) {
    this.target = target;
  }

  @Override
  @SneakyThrows
  public T process(ResultSet rs) {
    T obj=target.getDeclaredConstructor().newInstance();// Stu stu=new Stu();//new T()
    Field[] fields=target.getDeclaredFields();
    for(Field f:fields){
      f.setAccessible(true);
      f.set(obj,rs.getObject(f.getName()));
    }
    return obj;
  }
}
